package com.cpi.cornpp.futuresversions.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futuresversions.exception.CornFuturesVersionsException;
import com.cpi.cornpp.futuresversions.form.CornFuturesVersionsForm;
import com.cpi.cornpp.futuresversions.services.CornFuturesVersionsService;

/**
 * Corn Futures Versions Add Action
 * @author Edgar Joao
 *
 */
public class CornFuturesVersionsAddAction extends Action{
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$	
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Success new forward */
	private final static String SUCCESS_NEW_FORWARD = "success_new";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Parameter name */
	private static final String SAVE_AND_NEW_PARAM = "saveandnew";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	
	/**
	 * Metodo execute
	 * @author Edgar Joao
	 */	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws CornFuturesVersionsException {
		
		MessageResources messageResources = null;
		String messageError = null;						
		String forward = null;
		HttpSession httpSession = null;					
		CornFuturesVersionsService cornFuturesVersionsService = null;
		CornFuturesVersionsForm cornFuturesVersionsForm = null;		
		int saveAndNew = 0;
		
		try{
			saveAndNew = Integer.parseInt(request.getParameter(SAVE_AND_NEW_PARAM));
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			
			cornFuturesVersionsService = new CornFuturesVersionsService();
			if(form != null){
				cornFuturesVersionsForm = (CornFuturesVersionsForm) form;
				cornFuturesVersionsService.addCornFuturesVersions(cornFuturesVersionsForm.getCornFuturesVersionsView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
				if( saveAndNew == 1 ){
					forward = SUCCESS_NEW_FORWARD;
				}else{
					forward = SUCCESS_FORWARD;
				}
			}
			
		}catch (CornFuturesVersionsException cornFuturesVersionsException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, cornFuturesVersionsException);
			throw cornFuturesVersionsException;
		}
		catch (Exception e){
			CornFuturesVersionsException costElementException=null;
			e.printStackTrace(System.out);
			costElementException= new CornFuturesVersionsException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, costElementException);
			throw costElementException;
		} 		
		
		return mapping.findForward(forward);
	}
}
